package cn.zhuzicc.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.SQLOutput;
import java.time.LocalDate;
import java.util.Date;

/**
 * @author zx
 * @version 1.0
 * @className: Test08
 * @description: 获取运行时类对象的完整信息
 * @date: 2021/4/28 9:36
 */
public class Test08 {
    public static void main(String[] args) throws Exception {

        Class c1 = Class.forName("cn.zhuzicc.reflect.User");

        // 1. 获取类名
        // 1.1 获取类的全限定类名
        System.out.println(c1.getName());
        // 1.2 获取类的名称
        System.out.println(c1.getSimpleName()+"\n");

        // 2.获取类的属性
        // 2.1 获取类public修饰的属性
        Field[] fields = c1.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("\n");
        // 2.2 获取类的所有属性
        Field[] declaredFields = c1.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field);
        }
        System.out.println("\n");

        // 2.3 获取类的指定属性
        Field name = c1.getField("name");
        System.out.println("public修饰：" + name);
        Field age = c1.getDeclaredField("age");
        System.out.println("private修饰：" + age);
        System.out.println("\n");

        // 3. 获取类的方法
        System.out.println("3. 获取类的方法");
        // 3.1 获取本类及其父类的所有public方法
        System.out.println("3.1 获取本类及其父类的所有public方法:");
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        // 3.2 获取本类的所有方法
        System.out.println("3.2 获取本类的所有方法:");
        Method[] declaredMethods = c1.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method);
        }
        // 3.3 获取本类指定名称的方法
        System.out.println("3.3 获取本类指定名称的方法:");
        Method getName = c1.getMethod("getName");
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        // 4. 获取构造器
        System.out.println("4. 获取构造器");
        // 4.1 获取public修饰的构造器
        System.out.println("4.1 获取所有构造器:");
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        // 4.2 获取所有构造器
        System.out.println("4.2 获取所有构造器:");
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
        // 4.3 获取指定构造器
        System.out.println("4.3 获取指定构造器:");
        Constructor constructor = c1.getConstructor(String.class, Integer.class);
        System.out.println(constructor);

    }


}


class User{

    public String name;

    private Integer age;

    private LocalDate birthday;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public User(String name, Integer age, LocalDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }

    private void thisSecret(Boolean flag){
        if (flag) {
            System.out.println("你是我的朋友，你可以知道这个秘密");
        } else {
            System.out.println("这是一个不为人知的秘密");
        }
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}
